<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete">
      删除
    </el-button>

    <el-form ref="barcodeEditForm" :rules="barcodeRules" :model="barcode" label-width="80px" style="margin-left:0px;">
      <el-form-item label="货号" prop="sku">
        <el-input v-model="barcode.sku" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="尺码" prop="normalizedSize">
        <el-input v-model="barcode.normalizedSize" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="条形码" prop="barcode">
        <el-input v-model="barcode.barcode" style="width: 200px;" />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getBarcodeById, putBarcodeById, deleteBarcodeById } from '@/api/manager/goods-api'

export default {
  name: 'ManagerBarcodeEdit',
  filters: {
  },
  data() {
    return {
      barcode: {
        id: null,
        sku: null,
        normalizedSize: null,
        barcode: null
      },
      barcodeRules: {
        sku: [{
          required: true, message: '请输入货号', trigger: 'blur'
        }],
        normalizedSize: [{
          required: true, message: '请输入尺码', trigger: 'blur'
        }],
        barcode: [{
          required: true, message: '请输入条形码', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
    },
    loadDetail() {
      getBarcodeById(this.$route.params.id).then(response => {
        const { data } = response
        this.barcode = data
      })
    },

    handleSubmit() {
      this.$refs.barcodeEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.barcode))
        putBarcodeById(this.barcode.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 1 * 1000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteBarcodeById(this.barcode.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 1 * 1000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
